import btree4j.BTreeException;
import btree4j.template.Column;
import btree4j.template.Template;
import btree4j.template.TemplateManager;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class TemplateSecondaryTest {
    public static void main(String[] args) {
        File dataDir = new File("data");
        try {
            TemplateManager templateManager = new TemplateManager(dataDir);

            // 创建模板
            Template template1 = new Template("Users");
            template1.addColumn(new Column("id", "INT", true, false));
            template1.addColumn(new Column("name", "VARCHAR(100)", false, false));
            template1.addColumn(new Column("email", "VARCHAR(100)", false, true));
            long template1Id = templateManager.createTemplate(template1);
            System.out.println("Created template with ID: " + template1Id);

            Template template2 = new Template("Orders");
            template2.addColumn(new Column("order_id", "INT", true, false));
            template2.addColumn(new Column("user_id", "INT", false, false));
            template2.addColumn(new Column("amount", "DECIMAL(10,2)", false, false));
            template2.addColumn(new Column("status", "VARCHAR(20)", false, true));
            long template2Id = templateManager.createTemplate(template2);
            System.out.println("Created template with ID: " + template2Id);

            // 获取模板
            //Template retrievedTemplate = templateManager.getTemplate(template1Id);
            //System.out.println("Retrieved template: " + retrievedTemplate);

            //通过模版名获取模版
            Template retrievedTemplateByName = templateManager.getTemplateByName("Users");
            System.out.println("Retrieved template by name: " + retrievedTemplateByName);

            Template retrievedTemplateByName2 = templateManager.getTemplateByName("Orders");
            System.out.println("Retrieved template by name: " + retrievedTemplateByName2);

//            Template retrievedTemplate2 = templateManager.getTemplate(template2Id);
//            System.out.println("Retrieved template: " + retrievedTemplate2);

            // 获取所有模板
            List<Template> allTemplates = templateManager.getAllTemplates();
            System.out.println("All templates:");
            for (Template t : allTemplates) {
                System.out.println(t);
            }

//            // 更新模板
//            if (retrievedTemplateByName != null) {
//                retrievedTemplateByName.setName("UserProfiles");
//                templateManager.updateTemplate(retrievedTemplateByName);
//                Template updatedTemplate = templateManager.getTemplate(template1Id);
//                System.out.println("Updated template: " + updatedTemplate);
//            } else {
//                System.out.println("Template not found for ID: " + template1Id);
//            }

            //通过模版名获取更新后的模版
            Template retrievedTemplateByNameUpdate = templateManager.getTemplateByName("UserProfiles");
            System.out.println("Retrieved template by name after Update: " + retrievedTemplateByNameUpdate);

            // 删除模板
//            templateManager.deleteTemplate(template2Id);
//            System.out.println("Template deleted: " + template2Id);
//            System.out.println("Templates after deletion:");
//            for (Template t : templateManager.getAllTemplates()) {
//                System.out.println(t);
//            }

            //通过模版名删除模版
            templateManager.deleteTemplateByName("UserProfiles");
            System.out.println("Template deleted: " + template1Id);
            System.out.println("Templates after deletion:");
            for (Template t : templateManager.getAllTemplates()) {
                System.out.println(t);
            }

            templateManager.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BTreeException e) {
            throw new RuntimeException(e);
        }
    }
}